package com.victor.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.victor.common.annotion.Permission;
import com.victor.common.annotion.log.BussinessLog;
import com.victor.common.constant.Dict;
import com.victor.common.constant.tips.Tip;
import com.victor.common.controller.BaseController;
import com.victor.common.exception.BizExceptionEnum;
import com.victor.common.exception.BussinessException;
import com.victor.common.persistence.dao.WeddingDressMapper;
import com.victor.common.persistence.model.WeddingDress;
import com.victor.core.log.LogObjectHolder;
import com.victor.core.support.BeanKit;
import com.victor.core.util.ToolUtil;
import com.victor.modular.system.dao.WeddingDressDao;
import com.victor.modular.system.service.IWeddingDressService;
import com.victor.modular.system.transfer.WeddingDressDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@RequestMapping("/weddingDress")
@Controller
public class WeddingDressController extends BaseController {
    private static String PREFIX = "/system/weddingDress/";
    @Autowired
    private WeddingDressDao weddingDressDao;
    @Autowired
    private IWeddingDressService weddingDressService;
    @Autowired
    private WeddingDressMapper weddingDressMapper;
    /**
     * 跳转到 观众列表页面
     */
    @RequestMapping("")
    public String index(Model model) {
        return PREFIX + "weddingDress.html";
    }



    /**
     * 查询编码列表
     */
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(WeddingDressDto params) {
        List<Map<String, Object>> list = weddingDressDao.list(params);
        return list;
    }
    /**
     * 跳转到修改
     */
    @RequestMapping("/weddingDress_update/{id}")
    public String toEdit(@PathVariable Integer id, Model model) {
        WeddingDress weddingDress = weddingDressMapper.selectById(id);
        Map<String, Object> menuMap = BeanKit.beanToMap(weddingDress);
        model.addAttribute("weddingDress",menuMap);
        return PREFIX + "weddingDress_edit.html";
    }
    /**
     * 跳转到新增
     */
    @RequestMapping("/weddingDress_add")
    public String toAdd(Model model) {
        return PREFIX + "weddingDress_add.html";
    }
    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "/edit")
    @BussinessLog(value = "修改婚纱",  dict = Dict.WEDDINGDRESS_DICT)
    @Permission
    @ResponseBody
    public Tip update(WeddingDressDto params) throws Exception {
        if(ToolUtil.isEmpty(params)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        LogObjectHolder.me().set(weddingDressMapper.selectById(params.getId()));
        try {
            weddingDressService.updateWeddingDress(params);
        }catch (DuplicateKeyException e){
            throw new BussinessException(BizExceptionEnum.EXISTED_THE_AUDIENCE);
        }
            return SUCCESS_TIP;

    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/add")
    @BussinessLog(value = "新增婚纱",dict = Dict.WEDDINGDRESS_DICT)
    @Permission
    @ResponseBody
    public Tip add(WeddingDressDto params) throws Exception{
           weddingDressService.addWeddingDress(params);
            return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete/{id}")
    @BussinessLog(value = "删除婚纱",  dict = Dict.WEDDINGDRESS_DICT)
    @Permission
    @ResponseBody
    public Tip delete(@PathVariable Integer id) throws Exception {
        if(ToolUtil.isEmpty(id)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        weddingDressService.deleteWeddingDress(id);
        return SUCCESS_TIP;
    }


}
